package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JDBCDemo1 {
    public static void main(String[] args) throws Exception {
        /*
        1、加载驱动
        2、使用DriverManage建立连接->Connection对象
        3、使用Connection建立Statement对象
        4、使用Statement执行sql语句->ResultSet
        5、遍历结果集(前提是步骤4执行的是DQL语句，SELECT语句)
        6、关闭连接
        */

        //1、加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2、使用DriverManage建立连接->Connection
        // 三个参数：url、用户名、密码
        // url 格式：jdbc:mysql://主机名:端口号/数据库名?参数1&参数2&参数3
        // 默认端口号：3306
        String databaseName = "jdbc_study";
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/"+databaseName+"?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        System.out.println("连接成功");
        // 创建statement对象
        Statement statement = connection.createStatement();

        /*

        CREATE TABLE userinfo(\n" +
                        "            id INT PRIMARY KEY AUTO_INCREMENT,\n" +
                        "            username VARCHAR(32) NOT NULL,\n" +
                        "            password VARCHAR(32) NOT NULL,\n" +
                        "            nickname VARCHAR(32) NOT NULL,\n" +
                        "            age INT\n" +
                        "        )

         */
        // 使用statement对象创建表
        String sql = "CREATE TABLE userinfo(\n" +
                "            id INT PRIMARY KEY AUTO_INCREMENT,\n" +
                "            username VARCHAR(32) NOT NULL,\n" +
                "            password VARCHAR(32) NOT NULL,\n" +
                "            nickname VARCHAR(32) NOT NULL,\n" +
                "            age INT\n" +
                "        )";

        statement.execute(sql);
        System.out.println("创建表成功");




    }
}
